package cn.itcast.bos.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.RoleDao;
import cn.itcast.bos.dao.system.UserDao;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserService;
/**
 * 用户名验证方法的实现
 * @author 艾欣
 *
 */
@Service
@Transactional
public class UserServiceImpl implements UserService{
	//注入Dao层
	@Autowired
	private UserDao ud;
	//注入角色的Dao
	@Autowired
	private RoleDao rd;
	@Override
	public User findByUsername(String username) {
		User user = ud.findByUsername(username);
		return user;
	}
	@Override
	public List<User> findAll() {
		List<User> list = ud.findAll();
		return list;
	}
	@Override
	public void save(User model, String[] roleIds) {
		//先保存用户
		ud.save(model);
		//给用户赋予权限
		if(roleIds!=null){
			for (String roleId : roleIds) {
				//通过id查找出对应的角色
				 Role role = rd.findOne(Integer.parseInt(roleId));
				model.getRoles().add(role);
			}
		}
	}

}
